<?php

class Rcp_Methods_Request_AddApplicationPresent {
	
	/**
	 *
	 * @var Rcp_Methods_Data_App
	 */
	public $app;
	public $eventCalendarId;
	public $application;
	
	public function __construct($params) {
		$this->app = new Rcp_Methods_Data_App($params->app);
		$this->eventCalendarId = (int)$params->eventCalendarId;
		$this->application = new Rcp_Methods_Data_ApplicationRequest($params->application);
	}
	
	public function process() {
		$calendarEvent = RcpViewCalendarEventsQuery::create()
		->filterById($this->eventCalendarId)
		->filterByActive(true)
		->filterByEventRequestId(null, Criteria::ISNULL)
		->findOne();
		
		if (!($calendarEvent instanceof RcpViewCalendarEvents))
			throw new Rcp_Exception_App(Rcp_Exception_Codes::ERROR_CALENDAR_EVENT_NOT_EXIST, array($this->eventCalendarId), array('eventCalendarId' => $this->eventCalendarId));
		if ($calendarEvent->getEventRequestId() !== null)
			throw new Rcp_Exception_App(Rcp_Exception_Codes::ERROR_CALENDAR_EVENT_HAS_APPLICATION, array($this->eventCalendarId), array('eventCalendarId' => $this->eventCalendarId));
		
		
	
		$response = new Rcp_Methods_Response_GetCalendarEvents();
	
		foreach ($calendarEvents as $event) {
			if (!($event instanceof RcpCalendarEvents))
				continue;
			$this->setNullTime($calendarEvents, $event);
			$response->calendarEvents[] = new Rcp_Methods_Data_CalendarEvent($this->application->code);
		}
	
		return $response;
	}
	
}